package com.wantupai.mvp.framework.support.viewstate.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.wantupai.mvp.framework.support.activity.ActivityMvpDelegateImpl;
import com.wantupai.mvp.framework.support.manager.PresenterManager;
import com.wantupai.mvp.framework.support.viewstate.MvpViewStateDelegateCallback;
import com.wantupai.mvp.framework.support.viewstate.RestorableViewState;
import com.wantupai.mvp.framework.support.viewstate.ViewState;
import com.wantupai.mvp.mvp.MvpPresenter;
import com.wantupai.mvp.mvp.MvpView;


/**
 * @param <V>
 * @param <P>
 * @author zhangqing
 */
public class ActivityMvpViewStateDelegateImpl<V extends MvpView, P extends MvpPresenter<V>, VS extends ViewState<V>> extends ActivityMvpDelegateImpl<V, P>
{
    private MvpViewStateDelegateCallback<V, P, VS> mvpViewStateDelegateCallback;

    public ActivityMvpViewStateDelegateImpl(Activity activity, MvpViewStateDelegateCallback<V, P, VS> mvpViewStateDelegateCallback, boolean keepPresenterInstance)
    {
        super(activity, mvpViewStateDelegateCallback, keepPresenterInstance);
        this.mvpViewStateDelegateCallback = mvpViewStateDelegateCallback;
    }

    @Override
    public void onPostCreate(@Nullable Bundle savedInstanceState)
    {
        super.onPostCreate(savedInstanceState);
        VS viewState;
        if (viewId != null)
        {
            viewState = PresenterManager.getViewState(mActivity, viewId);
            if (viewState != null)
            {
                //缓存
                setViewState(viewState, true, true);
                return;
            }
        }
        viewState = mvpViewStateDelegateCallback.createViewState();
        if (viewState == null)
        {
            throw new NullPointerException("viewState不能为空,viewState没有创建");
        }
        if (savedInstanceState != null && viewState instanceof RestorableViewState)
        {
            RestorableViewState restorableViewState = (RestorableViewState) viewState;
            restorableViewState.onRestoreInstanceState(savedInstanceState);
            if (restorableViewState != null)
            {
                viewState = (VS) restorableViewState;
                setViewState(viewState, true, false);
                if (keepPresenterInstance)
                {
                    if (viewId == null)
                    {
                        throw new NullPointerException("viewId不能为空");
                    }
                    PresenterManager.putViewState(mActivity, viewId, viewState);
                }
                return;
            }
        }
        if (keepPresenterInstance)
        {
            if (viewId == null)
            {
                throw new NullPointerException("viewId不能为空");
            }
            PresenterManager.putViewState(mActivity, viewId, viewState);
        }
        setViewState(viewState, false, false);
        mvpViewStateDelegateCallback.onNewViewStateInstance();
    }

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        super.onSaveInstanceState(outState);
        boolean keepInstance = retainPresenterInstance(keepPresenterInstance, mActivity);
        VS viewState = mvpViewStateDelegateCallback.getViewState();
        if (viewState == null)
        {
            throw new NullPointerException("viewState不能为空");
        }
        if (keepInstance && viewState instanceof RestorableViewState)
        {
            ((RestorableViewState) viewState).onSaveInstanceState(outState);
        }

    }

    private void setViewState(@NonNull VS viewState, boolean applyViewState, boolean applyViewStateFromMemory)
    {
        if (viewState == null)
        {
            throw new NullPointerException("viewState不能为空");
        }
        mvpViewStateDelegateCallback.setViewState(viewState);
        if (applyViewState)
        {
            mvpViewStateDelegateCallback.setRestoringViewState(true);
            //具体的备份由子类决定
            mvpViewStateDelegateCallback.getViewState().apply(mvpViewStateDelegateCallback.getMvpView(), applyViewStateFromMemory);
            mvpViewStateDelegateCallback.setRestoringViewState(false);
            mvpViewStateDelegateCallback.onViewStateInstanceRestored(applyViewStateFromMemory);
        }
    }
}
